﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartMotion.Core;
using Sunny.UI;

namespace Station
{
    public partial class MCStationConfig : UIPage
    {
        private readonly StationBase _station;
        private UIComboTreeView _tree;
        public MCStationConfig(StationBase station)
        {
            _station = station;
            InitializeComponent();
        }

        private void MCStationConfig_Load(object sender, EventArgs e)
        {
            //grid.AllowUserToAddRows = false;
            SetGrid();
            _tree = new UIComboTreeView();
            _tree.NodeSelected += _tree_NodeSelected;
            _tree.Visible = false;
            grid.Controls.Add(_tree);

            for (int i = 0; i < _station.AxisesID.Count; i++)
            {
                var axis = GlobalVars.Ins.GetVar(_station.AxisesID[i]);
                object[] objs = { i, axis.Name, _station.AxisesID[i]};
                grid.Rows.Add(objs);
            }
        }

        private void SetGrid()
        {
            grid.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            grid.RowHeadersVisible = false;
            grid.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid.AllowUserToResizeRows = false;
            grid.MultiSelect = false;
            grid.AllowUserToAddRows = false;
            grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void _tree_NodeSelected(object sender, TreeNode node)
        {
            if (grid.CurrentCell == null) return;
            grid.CurrentCell.Value = node.Text;
           
            if (grid.Rows[grid.CurrentCell.RowIndex].Cells[grid.CurrentCell.ColumnIndex + 1].Value==null)//有无GUID
            {
                //添加
                _station.AxisesID.Add(node.Tag.ToString());
                AxisBase axis = GlobalVars.Ins.GetVar(node.Tag.ToString()).Value;
                _station.Axises.Add(axis);
            }
            grid.Rows[grid.CurrentCell.RowIndex].Cells[grid.CurrentCell.ColumnIndex + 1].Value = node.Tag.ToString();
            _station.AxisesID[grid.CurrentCell.RowIndex] = node.Tag.ToString();
        }   

        private void btnAdd_Click(object sender, EventArgs e)
        {
           int row= grid.Rows.Add();
           grid.Rows[row].Cells[0].Value = grid.Rows.Count-1;
        }

        private void grid_SelectIndexChange(object sender, int index)
        {
            AxisBase axis=null;
            if (grid.Rows[index].Cells[2].Value!=null)
            {
                string guid = grid.Rows[index].Cells[2].Value.ToString();
                axis = GlobalVars.Ins.GetVar(guid).Value;
            }
            if (axis!=null)
            {
                propertyGrid1.SelectedObject = axis;
            }
        }

        private void grid_CurrentCellChanged(object sender, EventArgs e)
        {
            if (grid.CurrentCell == null) return;
            if (grid.CurrentCell.ColumnIndex == 1)
            {
                if (grid.CurrentRow == null) return;
                GlobalVars.Ins.ComboTreeLoadData<AxisBase>(_tree);
                int columnIndex = grid.CurrentCell.ColumnIndex;
                int rowIndex = grid.CurrentCell.RowIndex;
                var rect = grid.GetCellDisplayRectangle(columnIndex, rowIndex, false);
                _tree.Left = rect.Left;
                _tree.Top = rect.Top;
                _tree.Width = rect.Width;
                _tree.Height = rect.Height;
                //将单元格的内容显示为下拉列表的当前项
                if (null != grid.Rows[rowIndex].Cells[columnIndex].Value)
                {
                    _tree.Text = grid.Rows[rowIndex].Cells[columnIndex].Value.ToString();
                }
                else
                {
                    _tree.Text = "";
                }
                _tree.Visible = true;
            }
            else
            {
                _tree.Visible = false;
            }
        }
    }
}
